package com 
{
	import com.adobe.serialization.json.JSON;
	import com.Logging.ClientAction;
	import com.Logging.ClientActionType;
	import data.*;
	
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCPlayerData 
	{
		static private var _id:int;						// Player's ID
		static private var _playerName:String;			// Player's name
		public var totalXp:int;							// Player's current experience
		public var lvlXp:int;							// Experience at current level
		public var playerLevel:int;						// Player's current level
		public var gold:uint;							// Amount of gold player has
		public var shipLocation:int;					// Player's current location
		public var shipInventory:GCInventory;			// Player's current inventory
		public var shipWorkers:uint;					// Number of workers who are currently on the ship
		public var _playerActiveShip:GCShip;			// Player's ship
		public var _playerAllShips:Vector.<GCShip>;		// All of a player's ships
		public var _quests:Vector.<GCQuest>;			// Orders the player has
		public var _tutorialStageAt:uint;				// The current stage of the tutorial at which the player current is
		public var totalWorkers:int;					// The total number of workers the player has
		public var availableWorkers:int;				// The number of currently unassigned workers
		// achievements
	
		public function GCPlayerData() 
		{
			_quests = new Vector.<GCQuest>();
			
			if (GCConstants.DEBUG_MODE)
			{
				// Dummy Values
				_id = 99;
				_playerName = "John";
				lvlXp = totalXp = 0;
				playerLevel = 1;
				gold = 100000;
				shipLocation = GCCityData.CITY_PICAROON;

				var upgrades:Dictionary = new Dictionary();
				upgrades[0] = GCItemData.ITEM_RUDDER;
				upgrades[1] = GCItemData.ITEM_CANNON;
				upgrades[2] = GCItemData.ITEM_COMPASS;
				
				var inventory:Array = new Array(new GCItem(1, GCItemData.ITEM_RECIPE_RUDDER, 1, 0, _id), new GCItem(2, GCItemData.ITEM_LUMBER, 10, 0, _id), new GCItem(3, GCItemData.ITEM_CROWS_NEST, 10, 0, _id), new GCItem(2, GCItemData.ITEM_FOOD, 100, 0, _id), new GCItem(15, GCItemData.ITEM_CANNON, 10, 0, _id));
				shipInventory = new GCInventory(new Vector.<GCItem>(), 0, 100);
				
				_playerActiveShip = new GCShip(0, GCShipData.SHIP_TYPE_SAILBOAT, upgrades);
				_playerAllShips = new Vector.<GCShip>();
				_playerAllShips.push(_playerActiveShip);
				
				upgrades = new Dictionary();
				upgrades[0] = GCItemData.ITEM_ANCHOR;
				upgrades[1] = GCItemData.ITEM_CANNON;
				upgrades[2] = GCItemData.ITEM_CROWS_NEST;
				_playerAllShips.push(new GCShip(1, GCShipData.SHIP_TYPE_BARGE, upgrades));
				// TODO: Get ships from DB for existing player, update DB for new player.
				
				_quests = new Vector.<GCQuest>();
				totalWorkers = 0;
				availableWorkers = 0;
				
				_tutorialStageAt = 0;//GCTutorial.STAGE_BUILD_GATHER;
			}
		}
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------
		public function getPlayerName():String
		{
			return _playerName;
		}
		
		public function getPlayerId():uint
		{
			return _id;
		}
		
		public function addQuest(quest:GCQuest):void
		{
			_quests.push(quest);
		}
		
		public function getQuests():Vector.<GCQuest>
		{
			return _quests;
		}
		
		public function get tutorialStage():uint
		{
			return _tutorialStageAt;
		}
		
		public function removeQuest(quest:GCQuest):void
		{
			if (GCConstants.LOGGING_DATA)
			{
				var o:Object = new Object();
				o["templateId"] = quest.templateId;
				GCPlayState.logger.LogAction(new ClientAction(ClientActionType.END_GOAL_OR_ORDER, o));
			}
			
			for (var i:int = 0; i < _quests.length; i++)
			{
				if (quest == _quests[i])
				{
					trace("removedQuest");
					_quests.splice(i, 1);
					break;
				}
			}
		}
		
		/*
		// Returns general stat values
		public function getStats():Array
		{
			return [id, playerName, xpLevel, playerLevel, gold];
		}
		
		// Returns player's current location
		public function getLocaion():int
		{
			return shipLocation;
		}
		
		// Returns the players inventory
		public function getInventory():Array
		{
			return shipInventory.copy();
		}
		
		// Returns the players
		*/
		
		/** Set Functions **/
		public function set id(pid:uint):void
		{
			_id = pid;
		}
		
		public function set tutorialStage(stage:uint):void
		{
			_tutorialStageAt = stage;
		}
		
		// Sets the players xp level
		public function setXPLevel(xp:int):void
		{
			totalXp = xp;
		}
		
		// Sets the players current level
		public function setPlayerLevel(level:int):void
		{
			playerLevel = level;
		}
		
		// Sets the players gold
		public function setGold(gold:uint):void
		{
			this.gold = gold;
		}
		
		// Sets the players ship location
		public function setLocation(location:int):void
		{
			shipLocation = location;			
		}
		
		public function setShipCollection(collection:Vector.<GCShip>):void
		{
			_playerAllShips = collection;
		}
		
		// Sets the players ship
		public function setShip(ship:GCShip):void
		{
			_playerActiveShip = ship;
		}
		
		public function get currentShip():GCShip
		{
			return _playerActiveShip;
		}
		
		
	}

}